/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StringInfoTable.java
 *
 * Created on Sep 5, 2011, 3:02:56 AM
 */
package operator.server.gui;

import share.*;
import java.awt.Graphics;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mire
 */
public class OperatorServerInfo extends javax.swing.JPanel {

    /** Creates new form StringInfoTable */
    public OperatorServerInfo(ConcurrentHashMap<Integer, Server> servers) {
        initComponents();
        this.servers = servers;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        fillTable();
        stringTable.repaint();
    }

    private void fillTable() {

        if (servers.isEmpty()) {
            return;
        }

        if (oldEntrySet != null) {
            if (oldEntrySet.size() == servers.size()) {
                return;
            }
        }
        HashSet<String> entrySet = new HashSet<String>();
        for (Iterator<Server> it = servers.values().iterator(); it.hasNext();) {
            Server server = it.next();
            entrySet.add(server.getAddress());
        }


        if (entrySet.equals(oldEntrySet)) {
            return;
        }

        DefaultTableModel model = (DefaultTableModel) stringTable.getModel();

        HashSet<String> toBeAdded = new HashSet<String>(entrySet);
        HashSet<String> toBeDeleted;
        if (oldEntrySet == null) {
            toBeDeleted = new HashSet<String>();
        } else {
            toBeDeleted = new HashSet<String>(oldEntrySet);

            toBeAdded.removeAll(oldEntrySet);
            toBeDeleted.removeAll(entrySet);
        }

        if (!toBeDeleted.isEmpty()) {
            for (int i = 0; i < model.getRowCount(); i++) {
                String data = (String) model.getValueAt(i, 0);
                if (toBeDeleted.contains(data)) {
                    model.removeRow(i);
                }
            }
        }

        if (!toBeAdded.isEmpty()) {
            Object[] objectRow = new Object[1];
            for (Iterator<String> it = toBeAdded.iterator(); it.hasNext();) {
                String data = it.next();
                objectRow[0] = data;
                model.addRow(objectRow);
            }
        }

        oldEntrySet = entrySet;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        stringTable = new javax.swing.JTable();

        stringTable.setAutoCreateRowSorter(true);
        stringTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Адреса"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        stringTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(stringTable);
        stringTable.getColumnModel().getColumn(0).setResizable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable stringTable;
    // End of variables declaration//GEN-END:variables
    private ConcurrentHashMap<Integer, Server> servers;
    private Set<String> oldEntrySet;
}
